define(function(require, exports, module) {
	'use strict';

    var rk = require('rk');
    var dataMgr = require('platform/manager/dataMgr')

    $.widget('rk.xsyPreViewRefer', {
        _params : {
                
        },
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var tpl = require('./tpl/xsyRefer.tpl');

            var childEntityId = opt.childEntityId;
            var widgetType = opt.widgetType;
            var config;
            config = rk.runDesignerMethod('#westPanel', 'westPanel', 'getItemInfo', widgetType, childEntityId);
           
            me._params.data.title = config ? config.label : '';
            var html = rk.templateText(tpl, me._params.data);
            elem.html(html);
            elem.attr('entityId', childEntityId);
        },
        _init : function(){}
    })


    $.widget('rk.xsyCustomizeRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_customize_icon',
                title : '',
                datas : [
                    {name : '示例名称',
                    items:[
                        {name:'创建日期', value:"2016-05-25"},
                        {name:'更新日期', value:"2016-05-25"},
                        {name:'负责人', value:"Jekyll"}
                    ]}
                ]
            }
        },
    })

    $.widget('rk.xsyAccountRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_contact_icon',
                title : window.SESSION.keywords.contact,
                datas : [
                    {name : '刘云',
                    items:[
                        {name:'客户类型', value:"默认业务类型"},
                        {name:'创建日期', value:"2016-05-25"},
                        {name:'负责人', value:"Jekyll"}
                    ]}
                ]
            }
        },
    });
    
    $.widget('rk.xsyPartnerRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_partner_icon',
                title : window.SESSION.keywords.contact,
                datas : [
                    {name : '刘云',
                    items:[
                        {name:'客户类型', value:"默认业务类型"},
                        {name:'创建日期', value:"2016-05-25"},
                        {name:'负责人', value:"Jekyll"}
                    ]}
                ]
            }
        },
    });
    $.widget('rk.xsyCompetitorRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_competitor_icon',
                title : window.SESSION.keywords.contact,
                datas : [
                    {name : '刘云',
                    items:[
                        {name:'客户类型', value:"默认业务类型"},
                        {name:'创建日期', value:"2016-05-25"},
                        {name:'负责人', value:"Jekyll"}
                    ]}
                ]
            }
        }
    });
    $.widget('rk.xsyProductRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_product_icon',
                title : window.SESSION.keywords.contact,
                datas : [
                    {name : '刘云',
                    items:[
                        {name:'客户类型', value:"默认业务类型"},
                        {name:'创建日期', value:"2016-05-25"},
                        {name:'负责人', value:"Jekyll"}
                    ]}
                ]
            }
        }
    });

    $.widget('rk.xsyContactRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_contact_icon',
                title : window.SESSION.keywords.contact,
                datas : [
                    {name : '刘云',
                    items:[
                        {name:'职位', value:"总经理"},
                        {name:'电话', value:"13333333333"},
                        {name:'负责人', value:"Jekyll"}
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyOppContactRefer', $.rk.xsyContactRefer, {
       
    });

    $.widget('rk.xsyOpportunityRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_opportunity_icon',
                title : window.SESSION.keywords.opportunity,
                datas : [
                    {name : 'CRM200人',
                    items:[
                        {name:'金额', value:"200,000元"},
                        {name:'阶段', value:"初步接洽"},
                        {name:'结单日期', value:"2016-05-25"}
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyContractRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_contract_icon',
                title : window.SESSION.keywords.contract,
                datas : [
                    {name : '合同标题1',
                    items:[
                        {name:'编号', value:"POS20160525"},
                        {name:'类型', value:"直销"},
                        {name:'折扣', value:"10%"}
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyOrderRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_order_icon',
                title : window.SESSION.keywords.order,
                datas : [
                    {name : 'POS20150525',
                    items:[
                        {name:'发货日期', value:"2016-05-25"},
                        {name:'状态', value:"审核通过"},
                        {name:'总金额', value:"200,000元"}
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyCaseRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_case_icon',
                title : window.SESSION.keywords.case,
                datas : [
                    {name : '审批无法通过',
                    items:[
                        {name:'个案编号', value:"20160525"},
                        {name:'状态', value:"处理中"},
                        {name:'未处理时间', value:"1天"}
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyScheduleRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_schedule_icon',
                title : window.SESSION.keywords.schedule + window.SESSION.keywords.task,
                datas : [
                    {name : '拜访刘经理',
                    items:[
                        {name:'截止日期', value:"2016-05-25"},
                        {name:'负责人', value:"Jekyll"}
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyApprovalRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_approval_icon',
                title : window.SESSION.keywords.approval,
                datas : [
                    {name : '审批主题xxxxxxxxx',
                    items:[
                        {name:'提交人', value:"Jekyll"},
                        {name:'类型', value:"报销审批"},
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyRescenterRefer', $.rk.xsyPreViewRefer, {
        _params:{
            data : {
                icon : 'xsy_su_refer_rescenter_icon',
                title : "文档",
                datas : [
                    {name : '公司合同模板20160525',
                    items:[
                        {name:'上传时间', value:"2016-05-25"},
                        {name:'上传人', value:"Jekyll"}
                    ]}
                ]
            }
        },
    });

    $.widget('rk.xsyPreViewHeader',{

        _params : {
            data : {
                items : [
                    {name:'类型', value:'示例类型'},
                    {name:'网址', value:'www.xiaoshouyi.com'},
                    {name:'电话', value:'400-050-0907'},
                    {name:'所属公海', value:'示例所属公海'},
                    {name:'客户负责人', value:'示例用户'}
                ]
            }
        },

        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var tpl = require('./tpl/xsyHeader.tpl');

            elem.addClass('xsy_su_header');

            me._params.data.icon = opt.icon ? opt.icon : '';
            me._params.data.name = opt.name ? opt.name : '';
            var html = rk.templateText(tpl, me._params.data);
            elem.html(html);

            if(!opt.name){
                $.post('/json/crm_customize/getLabel.action', {belongId : $.query('entityId')}, function(json){
                    if(json.data){
                        elem.find('.xsy_su_header_figure').html(json.data);
                    }
                })
            } 
        },

        _init : function(){

        }
    });

    $.widget('rk.xsyAccountHeader', $.rk.xsyPreViewHeader, {
        options : {
            icon : 'xsy_header_account',
            name : window.SESSION.keywords.account
        }
    })

    $.widget('rk.xsyCustomizeHeader', $.rk.xsyPreViewHeader, {
        options : {
            icon : 'xsy_header_customize',
            name: ''
        }
    })

    $.widget('rk.xsyOpportunityHeader', $.rk.xsyPreViewHeader, {
        options : {
            icon : 'xsy_header_opportunity',
            name : window.SESSION.keywords.opportunity
        }
    })

    /*$.widget('rk.xsyInfo', {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var tpl = require('./tpl/xsyAccountInfo.tpl');
            elem.html(tpl);
        },

        _init : function(){

        }
    })*/

    

    /*$.widget('rk.xsyAccountInfo', $.rk.xsyInfo, {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var tpl = require('./tpl/xsyAccountInfo.tpl');
            elem.html(tpl);
        },

        _init : function(){

        }
    });
*/

  /*$.widget('rk.xsyOpportunityInfo', $.rk.xsyInfo, {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var tpl = require('./tpl/xsyOpportunityInfo.tpl');
            elem.html(tpl);
        },

        _init : function(){

        }
    });*/
    
    $.widget('rk.xsyInfo', {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var formjson = dataMgr.getOriginalFormJson();

            $.getJSON('/admin/custom/openedit-pagelayout.action?linkPageLayoutId=' + formjson.config.linkPageLayoutId)
                .done(function(json) {
                    var tree = toTree(json);

                    me.render(tree);
                })
                .fail(function() {});

                function toTree(json){
                    var componentList = json.componentList;

                    var tree = [];
                    componentList.forEach(function(component){
                        if(component.componentType == 2){
                            var section = {};
                            section.label = component.componentLabel;
                            tree.push(section);
                        }else{
                            var section = tree.pop();
                            if(!section) section = {};
                            if(!section.itemList) section.itemList = [];

                            var item = {};
                            item.label = component.customItem.label;
                            item.id = component.customItem.id;
                            section.itemList.push(item);
                            tree.push(section);
                        }
                    })

                    return tree;
                }
        },

        render : function(componentList){
            var me = this;
            var elem = me.element;
            var opt = me.options;            
            var tpl = require('./tpl/xsyInfo.tpl');
            var html = rk.templateText(tpl, {componentList : componentList})
            elem.html(html);
        },

        previewRender : function(attributes){
            var me = this;
            var componentList = $.parseJSON(attributes.componentsJson);
            var tree = [];
            componentList.forEach(function(component){
                if(component.type == 2){
                    var section = {};
                    section.label = component.label;
                    tree.push(section);
                }else{
                    var section = tree.pop();
                    if(!section) section = {};
                    if(!section.itemList) section.itemList = [];

                    var item = {};
                    item.label = component.label;
                    item.id = component.itemId;
                    section.itemList.push(item);
                    tree.push(section);
                }
            });
            me.render(tree);

        },
    })

    $.widget('rk.xsyAccountInfo', $.rk.xsyInfo, {
           
    });
    $.widget('rk.xsyCustomizeInfo', $.rk.xsyInfo, {
        
    });

    $.widget('rk.xsyOpportunityInfo', $.rk.xsyInfo, {
      
    });

  

    $.widget('rk.xsyGroupMember', {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var tpl = require('./tpl/xsyGroupMember.tpl');
            elem.html(tpl);
        },

        _init : function(){

        }
    })

    $.widget('rk.xsyAccountHierarchy', {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var tpl = require('./tpl/xsyAccountHierarchy.tpl');
            elem.html(tpl);
        },

        _init : function(){

        }
    })

    $.widget('rk.xsyCrmFeed', {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            elem.addClass('feed-crm-detail');

            var tpl = require('./tpl/xsyFeed.tpl');
            elem.html(tpl);
        },

        _init : function(){

        }
    })

    $.widget('rk.xsyOpportunityProgress', {
        _create : function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var tpl = require('./tpl/xsyOpportunityProcess.tpl');

            elem.html(tpl);
        },
        _init : function(){

        }
    })


});